package com.ibeeking.found.cop.b.rest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.found.cop.b.rest.service.IMsgMessageSopDetailService;
import com.ibeeking.found.cop.service.common.dos.MsgMessageSopDetailDO;
import com.ibeeking.found.cop.service.common.param.MsgMessageSopDetailParam;
import com.ibeeking.found.cop.service.common.query.MsgMessageSopDetailQuery;
import com.ibeeking.found.cop.service.common.vo.MsgMessageSopDetailVO;
import com.ibeeking.found.cop.service.mapper.MsgMessageSopDetailMapper;
import com.ibeeking.found.id.GenerateId;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.json.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @ClassName MsgMessageSopDetailServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-01-27 18:50
 **/
@Service
public class MsgMessageSopDetailServiceImpl extends ServiceImpl<MsgMessageSopDetailMapper, MsgMessageSopDetailDO> implements IMsgMessageSopDetailService {

    @Resource
    private MsgMessageSopDetailMapper msgMessageSopDetailMapper;

    @Override
    public List<MsgMessageSopDetailVO> list(MsgMessageSopDetailQuery query) {
        List<MsgMessageSopDetailDO> entityDOS = msgMessageSopDetailMapper.selectList(new LambdaQueryWrapper<MsgMessageSopDetailDO>()
                .eq(query.getId() != null, MsgMessageSopDetailDO::getId, query.getId())
                .eq(query.getSopId() != null, MsgMessageSopDetailDO::getSopId, query.getSopId())
                .like(StringUtils.isNotBlank(query.getSendName()), MsgMessageSopDetailDO::getSendName, query.getSendName())
                .orderByDesc(MsgMessageSopDetailDO::getCreateTime)
        );
        List<MsgMessageSopDetailVO> entityVOS =JsonUtils.jsonToList(entityDOS, MsgMessageSopDetailVO.class);
        return entityVOS;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean add(MsgMessageSopDetailParam param) {
        MsgMessageSopDetailDO entityDO = BeanUtil.convertBean(param, MsgMessageSopDetailDO.class);
        entityDO.setId(GenerateId.generateId());
        entityDO.setCreateBy(UserUtils.getUserId());
        entityDO.setCreateTime(LocalDateTime.now());
        entityDO.setModifyTime(LocalDateTime.now());
        return msgMessageSopDetailMapper.insert(entityDO) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean updateBatch(List<MsgMessageSopDetailDO> entityDOS) {
       return super.updateBatchById(entityDOS);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean modify(MsgMessageSopDetailParam param) {
        MsgMessageSopDetailDO entityDO = BeanUtil.convertBean(param, MsgMessageSopDetailDO.class);
        entityDO.setModifyBy(UserUtils.getUserId());
        entityDO.setModifyTime(LocalDateTime.now());
        return msgMessageSopDetailMapper.updateById(entityDO) > 0;
    }

    @Override
    public MsgMessageSopDetailVO view(Long id) {
        MsgMessageSopDetailDO entityDO = msgMessageSopDetailMapper.selectById(id);
        if(entityDO == null) throw new BusinessException("没有查到相关记录！");
        MsgMessageSopDetailVO entityVO = BeanUtil.convertBean(entityDO, MsgMessageSopDetailVO.class);

        return entityVO;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean del(Long id) {
        MsgMessageSopDetailDO entityDO = msgMessageSopDetailMapper.selectById(id);
        if(entityDO == null) throw new BusinessException("没有查到相关记录！");
        return msgMessageSopDetailMapper.deleteById(id) > 0;
    }

}
